<?php

namespace app\admin\controller;


use think\Lang;
use think\Request;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Protocol extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/adminlog.lang.php');
    }

    public function index()
    {
        $res=\think\Db::table('ds_protocol')->select();
        $this->assign('protocol', $res);
        return $this->fetch();
    }

    public function edit(Request $request)
    {
        $id = $request->get('id', '');
        $protocol = \think\Db::table('ds_protocol')->where('id', '=', $id)->find();
        if ($request->isGet()) {
            /**
             * 模型实例化
             */
            $upload_model = model('upload');
            $condition['upload_type'] = '4';
            $condition['item_id'] = $protocol['id'];
            $file_upload = $upload_model->getUploadList($condition);
            if (is_array($file_upload)) {
                foreach ($file_upload as $k => $v) {
                    $file_upload[$k]['upload_path'] = $file_upload[$k]['file_name'];
                }
            }

            $this->assign('PHPSESSID', session_id());
            $this->assign('file_upload', $file_upload);
            $this->assign('protocoal', $protocol);
            $this->setAdminCurItem('edit');
            return $this->fetch();
        }

        $param = array();
        $param['id'] = intval(input('id'));
        $param['name'] = trim(input('name'));
        $param['content'] = trim(input('content'));
        $param['updated_at'] = date('Y-m-d H:i:s', time());
//        $document_model = model('pro');

        $result = \think\Db::table('ds_protocol')->where('id', '=', $id)->update($param);

        if ($result) {
            /**
             * 更新图片信息ID
             */
            $upload_model = model('upload');
            $file_id_array = input('post.file_id/a');
            if (is_array($file_id_array) && !empty($file_id_array)) {
                foreach ($file_id_array as $k => $v) {
                    $v = intval($v);
                    $update_array = array();
                    $update_array['upload_id'] = $v;
                    $update_array['item_id'] = intval(input('id'));
                    $upload_model->update($update_array);
                    unset($update_array);
                }
            }

            $this->log(lang('ds_edit') . '协议管理' . '[ID:' . input('id') . ']', 1);
//                    $this->success('编辑成功', 'protocol/index');
            dsLayerOpenSuccessNew(lang('ds_common_op_succ'), '/admin/protocol/index');

        } else {
            $this->error(lang('ds_common_save_fail'));
        }


    }

    public function ajax_update(Request $request)
    {
        $name = $request->get('name', '');
        $value = $request->get('value', '');
        if (empty($name)) {
            return json(['code' => 0, 'message' => '参数错误']);
        }

        $res = \think\Db::name('protocol')->where('title', '=', $name)->update(['content' => $value]);
        if ($res) {
            return json(['code' => 0, 'message' => '修改成功']);
        }

        return json(['code' => 1000, 'message' => '修改失败']);
    }

    /**
     * 系统文章图片上传
     */
    public function document_pic_upload() {
        /**
         * 上传图片
         */

        $file_name = '';
        $upload_file = BASE_UPLOAD_PATH . DS . ATTACH_ARTICLE . DS;
        $file_object = request()->file('fileupload');
        if ($file_object) {
            $info = $file_object->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file);
            if ($info) {
                $file_name = $info->getFilename();
            } else {
                echo $file_object->getError();
                exit;
            }
        } else {
            echo 'error';
            exit;
        }

        /**
         * 模型实例化
         */
        $upload_model = model('upload');
        /**
         * 图片数据入库
         */
        $insert_array = array();
        $insert_array['file_name'] = $file_name;
        $insert_array['upload_type'] = '4';
        $insert_array['file_size'] = $_FILES['fileupload']['size'];
        $insert_array['item_id'] = intval(input('param.item_id'));
        $insert_array['upload_time'] = time();
        $result = $upload_model->addUpload($insert_array);
        if ($result) {
            $data = array();
            $data['file_id'] = $result;
            $data['file_name'] = $file_name;
            $data['file_path'] = UPLOAD_SITE_URL.'/' . ATTACH_ARTICLE . '/'.$file_name;
            /**
             * 整理为json格式
             */
            $output = json_encode($data);
            echo $output;
        }
    }

    /**
     * ajax操作
     */
    public function ajax() {
        switch (input('param.branch')) {
            /**
             * 删除文章图片
             */
            case 'del_file_upload':
                if (intval(input('param.file_id')) > 0) {
                    $upload_model = model('upload');
                    /**
                     * 删除图片
                     */
                    $file_array = $upload_model->getOneUpload(intval(input('param.file_id')));
                    @unlink(BASE_UPLOAD_PATH . DS . ATTACH_ARTICLE . DS . $file_array['file_name']);
                    /**
                     * 删除信息
                     */
                    $condition = array();
                    $condition['upload_id'] = intval(input('param.file_id'));
                    $upload_model->delUpload($condition);
                    echo 'true';
                    exit;
                } else {
                    echo 'false';
                    exit;
                }
                break;
        }
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => lang('ds_manage'), 'url' => url('Protocol/index')
            ),
        );
        if (request()->action() == 'edit'){
            $menu_array[] = array(
                'name' => 'edit', 'text' => lang('ds_edit'), 'url' => 'javascript:void(0)'
            );
        }
        return $menu_array;
    }
}
